# -*- coding: utf-8 -*-
# @Time    : 2024/9/24 21:27
# @Author  : Alvin
# @File    : BussinessUtil.py
# @Des     : 业务工具类，处理一些业务需求，主要是减少主程序中的代码
import random
import time
import re
from toollib.guid import SnowFlake
import hashlib
from constants import Constants
from model.GetNumParam import GetNumParam
from datetime import datetime


def get_current_timetamp():
    return int(time.time() * 1000)


def get_encrypt_pwd(raw_pwd):
    temp_pwd = f"Alq_{raw_pwd}&%56"
    pwd = hashlib.md5(temp_pwd.encode(encoding='UTF-8')).hexdigest().upper()
    return pwd


def get_current_format_time():
    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    return current_time


def get_base_order_dict(baseId, param: GetNumParam, orderCost, user_id):
    """
    获取base_order_info 字典
    :param baseId:
    :param param:
    :param orderCost:
    :param user_id:
    :return:
    """
    order_base_info = {
        "id": baseId,
        "userId": user_id,
        "orderId": param.orderId,
        "mailNo": param.mailNo,
        "express": param.express,
        "receiveProvince": param.receiveProvince,
        "receiverCity": param.receiverCity,
        "receiverRegion": param.receiverRegion,
        "receiverAddressDetail": param.receiverAddressDetail,
        "receiveName": param.receiverName,
        "receivePhone": param.receiverPhone,
        "senderName": param.senderName,
        "senderPhone": param.senderPhone,
        "senderAddressDetail": param.sendAddressDetail,
        "orderCost": orderCost,
        "createTime": datetime.now(),
        "updateTime": datetime.now()
    }
    return order_base_info


def get_sync_order_dict(orderBaseId, param: GetNumParam, mailNo_gift, mailNo_self):
    """
    获取同步单字典
    :param orderBaseId:
    :param param:
    :param mailNo_gift:
    :param mailNo_self:
    :return:
    """
    sync_order_info = {
        "id": SnowFlake().gen_uid(),
        "orderBaseId": orderBaseId,
        "mailNo": param.mailNo,
        "express": param.express,
        "mailNo_gift": mailNo_gift,
        "express_gift": Constants.logistics_name,
        "mailNo_self": mailNo_self,
        "pickingCode": None,
        "pickingAddress": None,
        "hasPushPickingCode": False,
        "hasChangedOrder": 0,
        "statusCode": None,
        "statusStr": None,
        "yuYueShiJian": None,
        "logisticsInfo": None,
        "expressLastUpdateTime": None,
        "createTime": datetime.now(),
        "updateTime": datetime.now(),
        "pkCodeUpdateTime": None,
        "kdMobile": param.kdMobile,
    }
    return sync_order_info


def get_user_info_dict(user_name, mobile, balance):
    """
    获取新增用户字典
    :param user_name:
    :param mobile:
    :param balance:
    :return:
    """
    user_info = {
        "id": SnowFlake().gen_uid(),
        "user_name": user_name,
        "user_code": user_name,
        "mobile": mobile,
        "balance": balance,
        "create_time": datetime.now()
    }
    return user_info


def get_delay_order_dict(orderBaseId, mailNo_gift, express_gift, logisticsType, min_distance_data):
    """
    获取延迟单字典
    :return:
    """
    expressId = min_distance_data.get("id", None)
    mailNo = min_distance_data.get("mailNo", None)
    express = min_distance_data.get("express", None)
    delay_order_info = {
        "id": SnowFlake().gen_uid(),
        "orderBaseId": orderBaseId,
        "expressId": expressId,
        "mailNo": mailNo,
        "express": express,
        "mailNo_gift": mailNo_gift,
        "express_gift": express_gift,
        "logisticsType": logisticsType,
        "statusCode": None,
        "statusStr": None,
        "logisticsInfo": None,
        "expressLastUpdateTime": None,
        "od_link_id": None,
        "od_basic_id": None,
        "b_link_id": None,
        "createTime": datetime.now(),
        "updateTime": datetime.now()
    }
    return delay_order_info


def get_gift_order_dict(orderBaseId, mailNo_gift, express_gift, logisticsType, min_distance_data):
    """
    获取礼品单字典
    :return:
    """
    expressId = min_distance_data.get("id", None)
    mailNo = min_distance_data.get("mailNo", None)
    express = min_distance_data.get("express", None)
    delay_order_info = {
        "id": SnowFlake().gen_uid(),
        "orderBaseId": orderBaseId,
        "expressId": expressId,
        "mailNo": mailNo,
        "express": express,
        "mailNo_gift": mailNo_gift,
        "express_gift": express_gift,
        "logisticsType": logisticsType,
        "statusCode": None,
        "statusStr": None,
        "logisticsInfo": None,
        "expressLastUpdateTime": None,
        "od_link_id": None,
        "od_basic_id": None,
        "b_link_id": None,
        "createTime": datetime.now(),
        "updateTime": datetime.now()
    }
    return delay_order_info


def get_shua_order_dict(orderBaseId, mailNo_gift, express_gift, logisticsType, min_distance_data):
    """
    获取S单字典
    :return:
    """
    expressId = min_distance_data.get("id", None)
    mailNo = min_distance_data.get("mailNo", None)
    express = min_distance_data.get("express", None)
    shua_order_info = {
        "id": SnowFlake().gen_uid(),
        "orderBaseId": orderBaseId,
        "expressId": expressId,
        "mailNo": mailNo,
        "express": express,
        "mailNo_gift": mailNo_gift,
        "express_gift": express_gift,
        "logisticsType": logisticsType,
        "statusCode": None,
        "statusStr": None,
        "logisticsInfo": None,
        "expressLastUpdateTime": None,
        "od_link_id": None,
        "od_basic_id": None,
        "b_link_id": None,
        "createTime": datetime.now(),
        "updateTime": datetime.now()
    }
    return shua_order_info


def get_logistics_default_num():
    if Constants.logistics_name == "惊云物流":
        return f"JTWL{str(int(time.time() * 10000))}"
    else:
        return f"CYWL{str(int(time.time() * 10000))}"


def format_logistics_num_string(input_number):
    # 确保输入是字符串类型
    if not isinstance(input_number, str):
        input_number = str(input_number)

    # input_number_str = re.sub(r'\d+', '', input_number)

    # 去除输入字符串中的所有非数字字符（如果需要）
    input_number_str = ''.join(filter(str.isdigit, input_number))

    # 处理数字字符串，使其长度为14位
    # input_number_str = input_number_str[-14:].zfill(14)

    # 拼接前缀和处理后的数字字符串
    result = "CYWL" + input_number_str

    return result


def is_valid_phone(phone: str) -> bool:
    """
    校验中国大陆手机号格式是否合格

    参数:
    phone (str): 待校验的手机号字符串

    返回:
    bool: 格式合格返回True，不合格返回False
    """
    # 定义手机号正则表达式模式
    pattern = r'^1[3-9]\d{9}$'

    # 进行匹配验证
    return bool(re.match(pattern, phone))


def generate_random_digits():
    # 第一位数字从1-9中选择
    first_digit = random.choice('123456789')
    # 后面11位数字可以是0-9中的任意数字
    remaining_digits = ''.join(random.choices('0123456789', k=11))
    return first_digit + remaining_digits


def jude_str_is_have_chinese(content: str):
    """
    判断字符串里面是否包含中文
    """
    pattern = re.compile(r'[\u4e00-\u9fff]')
    return bool(pattern.search(content))
# import re
# if __name__ == '__main__':
#     mailNo = "YT8752716089659"
#     # result_string = re.sub(r'\d+', '', mailNo)
#     result = format_logistics_num_string(mailNo)
#     print(result)
